import Vue from 'vue'
import common from '../utils/common'
import Vuex from 'vuex'
import http from '../utils/request'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    //是否登录
    whetherLogin: common.getCookie('materialsToken'),
    userName: localStorage.getItem('userName'),
    userId: localStorage.getItem('userId'),
    // 当前登录人拥有的权限
    menuList: [],
    // 当前登录人所拥有的个人中心菜单权限
    personalMenuList: [],
    // 待办事项数量
    upcomingNum: 0
  },
  mutations: {
    setWhetherLogin(state, whetherLogin) {
      state.whetherLogin = whetherLogin
    },
    setUserName(state, userName) {
      state.userName = userName
    },
    setUserId(state, userId) {
      state.userId = userId
    },
    setUpcoming(state, payload) {
      state.upcomingNum = payload.length
    },
    setMenuList(state, payload) {
      // 该账号所有权限
      state.menuList = payload
      // 个人中心菜单权限
      state.personalMenuList = payload.filter(item => {
        return item.title !== '城投公司看板' && item.title !== '个人中心'
      })
    }
  },
  actions: {
    // 获取权限菜单
    async getMenuList(store, payload) {
      await http({
        method: 'get',
        url: '/api/getMenu',
        data: {}
      })
        .then(res => {
          if (res.code === 0) {
            store.commit('setMenuList', res.data)
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 获取待办事项数量
    getUpcoming(store, payload) {
      http({
        method: 'post',
        url: '/todoList/handleList',
        data: {}
      })
        .then(res => {
          if (res.code === 0) {
            store.commit('setUpcoming', res.data || [])
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  },
  getters: {
    getWhetherLogin: state => state.whetherLogin,
    getUserName: state => state.userName,
    getUserId: state => state.userId,
    getMenuList: state => state.menuList,
    getUpcoming: state => state.dbValue
  },
  modules: {}
})
